package com.ruoyi.merchant.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.db.domain.MerchantBaseInfo;
import com.ruoyi.db.domain.MerchantOperatorLog;
import com.ruoyi.db.domain.MerchantProductLaunchInfo;
import com.ruoyi.db.mapper.MerchantBaseInfoMapper;
import com.ruoyi.db.mapper.MerchantOperatorLogMapper;
import com.ruoyi.db.mapper.MerchantProductLaunchInfoMapper;
import com.ruoyi.laiqianba.service.IHandleCacheService;
import com.ruoyi.merchant.service.IMerchantBaseInfoService;
import com.ruoyi.merchant.service.IMerchantProductLaunchInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 商户基本信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-12
 */
@Service
@DataSource(DataSourceType.SLAVE)
//@DS("slave")
public class MerchantBaseInfoServiceImpl extends ServiceImpl<MerchantBaseInfoMapper, MerchantBaseInfo> implements IMerchantBaseInfoService {

    @Resource
    private MerchantBaseInfoMapper merchantBaseInfoMapper;

    @Resource
    private MerchantOperatorLogMapper merchantOperatorLogMapper;

    @Autowired
    IHandleCacheService handleCacheService;
    @Autowired
    MerchantProductLaunchInfoMapper merchantProductLaunchInfoMapper;
    @Autowired
    MerchantProductLaunchInfoMapper productLaunchInfoMapper;

    /**
     * 查询商户基本信息
     *
     * @param id 商户基本信息主键
     * @return 商户基本信息
     */
    @Override
    public MerchantBaseInfo selectMerchantBaseInfoById(Long id) {
        return merchantBaseInfoMapper.selectMerchantBaseInfoById(id);
    }

    /**
     * 查询商户基本信息列表
     *
     * @param merchantBaseInfo 商户基本信息
     * @return 商户基本信息
     */
    @Override
    public List<MerchantBaseInfo> selectMerchantBaseInfoList(MerchantBaseInfo merchantBaseInfo) {
        return merchantBaseInfoMapper.selectMerchantBaseInfoList(merchantBaseInfo);
    }

    /**
     * 查询商户基本信息列表
     *
     * @param merchantBaseInfo 商户基本信息
     * @return 商户基本信息
     */
    @Override
    public List<Map<String, Object>> selectMerchantBaseInfoMapList(MerchantBaseInfo merchantBaseInfo) {
        return merchantBaseInfoMapper.selectMerchantBaseInfoMapList(merchantBaseInfo);
    }

    /**
     * 新增商户基本信息
     *
     * @param merchantBaseInfo 商户基本信息
     * @return 结果
     */
    @Override
    public int insertMerchantBaseInfo(MerchantBaseInfo merchantBaseInfo) {
        merchantBaseInfo.setCreateTime(DateUtils.getNowDate());
        merchantBaseInfo.setStatus(1L);
        return merchantBaseInfoMapper.insertMerchantBaseInfo(merchantBaseInfo);
    }

    @Override
    public MerchantBaseInfo saveMerchantBaseInfo(MerchantBaseInfo merchantBaseInfo) {
        int insert = merchantBaseInfoMapper.insert(merchantBaseInfo);
        if (insert > 0) {
            return merchantBaseInfo;
        }
        return null;
    }

    /**
     * 修改商户基本信息
     *
     * @param merchantBaseInfo 商户基本信息
     * @return 结果
     */
    @Override
    public int updateMerchantBaseInfo(MerchantBaseInfo merchantBaseInfo) {

        MerchantOperatorLog merchantOperatorLog = new MerchantOperatorLog();
        merchantOperatorLog.setInfoId(merchantBaseInfo.getId());
        merchantOperatorLog.setOperatorId(SecurityUtils.getUserId());
        merchantOperatorLog.setOperatorName(SecurityUtils.getUsername());
        merchantOperatorLog.setType(0L); // 0代表是机构
        merchantOperatorLog.setOperationType(merchantBaseInfo.getStatus());
        merchantOperatorLog.setCreateTime(DateUtils.getNowDate());
        merchantOperatorLogMapper.insertMerchantOperatorLog(merchantOperatorLog);

        String priceType = merchantBaseInfo.getPriceType();
        if (StrUtil.equals(priceType, "1")) {
            merchantBaseInfo.setTypeRate("0");
        }
        merchantBaseInfo.setUpdateTime(DateUtils.getNowDate());
        if(merchantBaseInfo.getCompanyPhone()!=null || merchantBaseInfo.getStatus() != null){
            MerchantProductLaunchInfo merchantProductLaunchInfo = new MerchantProductLaunchInfo();
            merchantProductLaunchInfo.setInfoId(merchantBaseInfo.getId());
            merchantProductLaunchInfo.setPrivacyPolicy(merchantBaseInfo.getCompanyPhone());
            merchantProductLaunchInfo.setStatus(merchantBaseInfo.getStatus());
            merchantProductLaunchInfoMapper.updateMerchantProductLaunchInfo(merchantProductLaunchInfo);
        }
        int i = merchantBaseInfoMapper.updateMerchantBaseInfo(merchantBaseInfo);

        if (i>0){
            handleCacheService.clearMerchantBaseInfo(merchantBaseInfo.getId());
        }

        return i;
    }

    /**
     * 批量删除商户基本信息
     *
     * @param ids 需要删除的商户基本信息主键
     * @return 结果
     */
    @Override
    public int deleteMerchantBaseInfoByIds(Long[] ids) {
        return merchantBaseInfoMapper.deleteMerchantBaseInfoByIds(ids);
    }

    /**
     * 删除商户基本信息信息
     *
     * @param id 商户基本信息主键
     * @return 结果
     */
    @Override
    public int deleteMerchantBaseInfoById(Long id) {
        return merchantBaseInfoMapper.deleteMerchantBaseInfoById(id);
    }

    @Override
    public Long seletInfoIdBydeptId(Long deptId) {
        return merchantBaseInfoMapper.seletInfoIdBydeptId(deptId);
    }

    @Override
    public void testMapper() {
        List<MerchantBaseInfo> merchantBaseInfos = this.baseMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public MerchantBaseInfo selectMerchantBaseInfoByDeptId(Long deptId) {
        return merchantBaseInfoMapper.selectMerchantBaseInfoByDeptId(deptId);
    }

    @Override
    public List<Map<String, String>> getChantBaseInfoId() {
        return merchantBaseInfoMapper.getChantBaseInfoId();
    }


}
